<template>
    <div class="login">
        <div class="loginBox">
            <p class="title">乡村管理后台</p>
            <el-form :model="ruleForm" label-position="right" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">

                <el-form-item label="帐号" prop="username">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off"/>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" @keyup.enter.native="submitForm('ruleForm')" v-model="ruleForm.password" autocomplete="off"/>
                </el-form-item>

            </el-form>
            <p class="login_button">
                <el-button type="primary" @click="submitForm('ruleForm')" size="medium" >登录</el-button>
            </p>
        </div>
        <div @click="aaaa" class="login_positon">粤ICP备19019135号</div>
    </div>
</template>
<script>
    import axios from 'axios';
    export default {
        data() {

            const validateUserName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入帐号'));
                } else {
                    callback();
                }
            };
            const validatePassWord = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        {type: 'Number', validator: validateUserName, trigger: 'blur'}
                    ],
                    password: [
                        {type: 'Number', validator: validatePassWord, trigger: 'blur'}
                    ]
                }
            };
        },
        created() {
            
        },
        methods: {
            aaaa() {
                window.open('http://www.miitbeian.gov.cn')
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.isLogin();
                    } else {
                        return false;
                    }
                });
            },
            created() {
                 document.onkeydown = function(event){
                    var e = event || window.event || arguments.callee.caller.arguments[0];
                   if(e && e.keyCode==27){ // 按 Esc 
                    //要做的事情
                      }
                    if(e && e.keyCode==113){ // 按 F2 
                         //要做的事情
                    }            
                     if(e && e.keyCode==13){ // enter 键
                      //要做的事情
                     }

                 }; 
            },
            isLogin() {
                let username = this.ruleForm.username;
                let password = this.$md5(this.ruleForm.password);
                let data = {
                    username: username,
                    password: password,
                };
                let url1 = `/admin/login/account`;
                // axios.post(url1,data).then(res=> {
                //     let user = res.data.data;
                //     console.log(res);
                //     localStorage.setItem("username", user.username)
                //     localStorage.setItem("headUrl", user.headUrl)
                //     localStorage.setItem("token", user.token)
                //     localStorage.setItem("userId", user.userId)
                //     this.$router.push('/article/list')     
                // })

                // this.$request.post(url,data).then(res=> {
                //     let user = res.data.data;
                //     console.log(res);
                //     localStorage.setItem("username", user.username)
                //     localStorage.setItem("headUrl", user.headUrl)
                //     localStorage.setItem("token", user.token)
                //     localStorage.setItem("userId", user.userId)
                //     this.$router.push('/article/list')
                // })
                this.$request.lang(url1, data).then(res => {
                    console.log(res)
                    let user = res.data.data;
                    //code码可在公共方法里以统一处理
                    if (res.data.code == "0000" && res.data.data.userId > 0) {
                        // 将登录用户信息缓存到localStorage
                        // headUrl:"https://testsyhbusiness-1253703708.cos.ap-guangzhou.myqcloud.com/icon/201808/08df2f0f-82bd-4592-80b2-07bbe26ca3b1.jpg"
                        // token:"50d70aae9779e391fb53c6e5499b18fb"
                        // userId:1
                        // username:"admin"
                        localStorage.setItem("username", user.username)
                        localStorage.setItem("headUrl", user.headUrl)
                        localStorage.setItem("token", user.token)
                        localStorage.setItem("userId", user.userId)
                        this.$router.push({
                            path: '/article/list',
                        })
                    }else {
                        this.$message.error(res.data.message)
                    }

                }).catch(err => {
                    console.log('err',err);
                });
            }
        }
    }
</script>
<style scoped lang="less">
    .login_positon {
        position: absolute;
        left: 50%;
        top: 88%;
        transform: translate(-50%)
    }
    .loginBox {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 500px;

        .demo-ruleForm {
            padding-right: 40px
        }

        .login_button {
            margin-bottom: 20px;
            text-align: center;
        }

        .el-form-item {
            margin: 40px 0;
        }
    }

    .title {
        color: #fff;
        line-height: 50px;
        background-color: #409EFF;
        height: 50px;
        text-align: center;
    }

    .loginBox {
        border: 1px solid #ebebeb;
    }
</style>
